﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace WebSocketClientApp
{
    public partial class Form2 : Form
    {
        private WebSocketClient2 _client;
        public Form2()
        {
            InitializeComponent();
            _client = new WebSocketClient2();
        }

        private async void btn_connect_Click(object sender, EventArgs e)
        {
            try
            {
                await _client.ConnectAsync();
                MessageBox.Show("WebSocket 客户端已连接！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"连接失败：{ex.Message}");
            }
        }

        private async void btn_send_Click(object sender, EventArgs e)
        {
            string message = txt_send.Text;
            try
            {
                await _client.SendMessageAsync(message);
                MessageBox.Show("消息已发送！");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发送失败：{ex.Message}");
            }
        }

        private async void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            await _client.DisconnectAsync();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            try
            {
                //获取程序的启动路径
                string my_str_StartPath = System.Windows.Forms.Application.StartupPath;
                string my_str_iniPath = my_str_StartPath + "\\ini\\ini.xml";
                try
                {
                    XmlTextReader rdr = new XmlTextReader(my_str_iniPath);
                    while (rdr.Read())
                    {
                        if (rdr.NodeType == XmlNodeType.Element)
                        {
                            //通过rdr.Name得到节点名
                            string elementName = rdr.Name;
                            if (elementName == "root")
                            {
                            }
                            else if (elementName == "service_port")
                            {
                                //可以通过中括号获得属性值
                                //string colorVal = rdr["color"];
                                //读取到节点内文本内容
                                if (rdr.Read())
                                {
                                    //通过rdr.Value获得文本内容
                                    string my_service_port = rdr.Value;
                                    int my_int_service_port = 8080;
                                    if (int.TryParse(my_service_port, out my_int_service_port))
                                    {

                                    }
                                    InfoClass.servicePort = my_int_service_port;
                                }
                            }
                           
                        }
                        else if (rdr.NodeType == XmlNodeType.EndElement)
                        {
                            //在节点结束时也可以通过rdr.Name获得节点名字
                            //string elementName = rdr.Name;
                            //Console.WriteLine(elementName + " element end");
                        }
                    }
                }
                catch (Exception ex)
                {
                    //输出日志
                    LogHelper.WriteLog("读取配置文件发生异常-》"+ex);
                }

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (var popup = new PopupForm("我是提示", 3000))
            {
                popup.StartPosition = FormStartPosition.CenterScreen;
                popup.ShowDialog(); // 或者使用 Show() 方法，取决于是否需要模态窗口
            }
        }
    }
}
